package com.jysz.certificate.service.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.Constants;
import com.jysz.certificate.persistence.dao.system.UserDao;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.dto.UserDTO;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.utils.CommonUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class UserService extends EntityService<User, Long, UserDao> {

	@Resource
	private UserDao userDao;

	public List<User> getList() {
		return userDao.findAll();
	}

	public User getUserByName(String name) {
		return userDao.getUserByName(name);
	}
	
	public User getUserByProperty(String property,String value) {
		return userDao.getUserByProperty(property,value);
	}

	public void saveUser(User user) {
		user.setEntryDate(new Date());
		userDao.save(user);
	}
	
	public void updateUser(User user){
		userDao.merge(user);
	}

	public void deleteUser(User user) {
		user = userDao.find(user.getId());
		user.setLocked("1");
		userDao.save(user);
	}
	
	public User finUserById(Long id){
		return userDao.find(id);
	}
	
	public User getPassUserByJobNumber(User user) {
		Criteria criteria = userDao.createCriteria();
		criteria.add(Restrictions.ne("id", user.getId()));
		criteria.add(Restrictions.eq("jobNumber", user.getJobNumber()));
		List<User> list = criteria.list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		} 
		return null;
	}


	public Page<User> selectUser(UserDTO search, int currentPage, int pageSize) {
		Criteria criteria = userDao.createCriteria();
		criteria.addOrder(Order.asc(User.COLUMN_LOCKED));
		if (null != search) {
			if (null != search.getRealName()
					&& !"".equals(search.getRealName())) {
				criteria.add(Restrictions.like("realName",
						search.getRealName(), MatchMode.ANYWHERE));
			}
			if (search.getPost()>-1) {
				criteria.add(Restrictions.eq("post", PostType.index(search.getPost())));
			}
			// 职位
		}
		return this.findPage(criteria, currentPage, pageSize);
	}

	@Override
	public UserDao getEntityDao() {
		return userDao;
	}

	public Page<User> findPage(int pageNo, int pageSize) {
		return this.findPage(userDao.createCriteria(), pageNo, pageSize);
	}

	public boolean auth(User userInfo) {
		userInfo = findAuthorizedUser(userInfo);
		return userInfo != null ? true : false;
	}

	public User findAuthorizedUser(User userInfo) {
		if(userInfo == null){
			return null;
		}
		userInfo = userDao
				.findUnique(Restrictions.eq(User.COLUMN_LOCKED,
						Constants.SYS_USER_LOCK_NO), Restrictions.eq(
						User.COLUMN_NAME, userInfo.getName()), Restrictions.eq(
						User.COLUMN_PASSWORD, userInfo.getPassword()));
		return userInfo;
	}
	/**
	 * 查询工号
	 */
	public boolean getUserByJobNumber(String jobNumber) {
		if(userDao.getUserByJobNumber(jobNumber) != null){
			return false;
		}
		return true;
	}
	public Map<String, User> userMap(){
		Criteria criteria=userDao.createCriteria();
		List<User> users = criteria.list();
		Map<String, User> userMap = new HashMap<String, User>();
		for(int i=0;i<users.size();i++){
			User user = users.get(i);
			userMap.put(user.getJobNumber(), user);
		}
		return userMap;
	}
	
	/**
	 * 选人
	 * @param user
	 * @return
	 */
	public Page<User> choose(User user,int pageNum,int pagesize){
		Criteria criteria=userDao.createCriteria();
		if(user!=null&&user.getPost()!=null)//这里将注释解除，因为在角色选择的时候只会出现 驻店员
			criteria.add(Restrictions.eq("post", PostType.Shop));
		if(user!=null&&user.getRealName()!=null&&!user.getRealName().equals(""))
			criteria.add(Restrictions.like("realName", user.getRealName(),MatchMode.ANYWHERE));
		return this.findPage(criteria, pageNum, pagesize);
	}
	/**
	 * 此方法用于角色选择人员，
	 * @param user
	 * @param pageNum
	 * @param pagesize
	 * @return
	 */
	public Page<User> roleChoose(User user,int pageNum,int pagesize){
		Criteria criteria=userDao.createCriteria();
		if(user!=null&&user.getPost()!=null)
				criteria.add(Restrictions.eq("post", PostType.Shop));
		return this.findPage(criteria, pageNum, pagesize);
	}
	/**
	 * 绩效人员名单选人
	 * @param user
	 * @return
	 */
	public Page<User> pmUserChoose(User user,int pageNum,int pagesize){
		Criteria criteria=userDao.createCriteria();
		if(user!=null&&user.getPost()!=null)
				criteria.add(Restrictions.eq("post", PostType.Shop));
		return this.findPage(criteria, pageNum, pagesize);
	}
	
	/**
	 * 选人
	 * @param user
	 * @return
	 */
	public Page<User> chooseUser(User user,int post,int pageNum,int pagesize){
		Criteria criteria=userDao.createCriteria();
		if(user!=null){
			if(user.getRealName()!=null&&!"".equals(user.getRealName())){
				criteria.add(Restrictions.like("realName", user.getRealName(),MatchMode.ANYWHERE));
			}
		}
		criteria.add(Restrictions.eq("locked","0"));
			if(post==1){
				criteria.add(Restrictions.eq("post", PostType.Shop));
			}else{
				criteria.add(Restrictions.ne("post",PostType.Admin)).add(Restrictions.ne("post", PostType.Shop));
			}
		
		return this.findPage(criteria, pageNum, pagesize);
	}
	/**
	 * 库存查询，获得区域经理名字
	 * @param id
	 * @return
	 */
	public String selectName(Long id){
		Criteria criteria=userDao.createCriteria();
		criteria.add(Restrictions.eq("post", PostType.Manager));
		criteria.add(Restrictions.eq("id", id));
		User user = (User) criteria.uniqueResult();
		if(user != null){
			return user.getRealName();
		}
		return null;
	}
	
	
	/**
	 * 根据用户ids获得名称,以逗号隔开
	 * @author xuxiaolong
	 * @param ids
	 * @return
	 */
	public String getUserNames(String ids){
		
		StringBuilder sb=new StringBuilder();
		if(CommonUtil.isNotEmpty(ids)){
			List<User> list=super.findByIds(CommonUtil.getLongList(ids));
			for(User u:list){
				sb.append(u.getRealName()).append(",");
			}
			if(sb.length()>1)
				sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString();
	}

}
